
#ifndef _SINGLELINK_H_
#define _SINGLELINK_H_

#include "node.h"

	/* This class is the basis for all the single linked strutures
	   like lists, stacks and queues. */

template <class e>
class SingleLink 
{
	protected:
		Node<e,1,1> *head, *tail, *temp;

	public:
		SingleLink();

		~SingleLink();

			// Inserts the element to the end of the list
		void Insert (e element);
		
			// Locate an element in the list
		e At (int index);
		e operator[] (int index);

			// Get the number of elements in the link list
		int GetSize();
};

template <class e>
SingleLink<e>::SingleLink()
{
	head = NULL;
	tail = NULL;
}

template <class e>
SingleLink<e>::~SingleLink()
{
	while (head != NULL)
	{
		temp = head;
		head = head -> next();
		delete temp;
	}
}

template <class e>
void SingleLink<e>::Insert (e element)
{
	temp = new Node<e,1,1>(element);
	if (head == NULL)
	{
		head = temp;
	}
	else
	{
		tail->SetNext (temp);
	}
	tail = temp;
}

template <class e>
e SingleLink<e>::At (int index)
{
	int i;
	for (temp = head, i = 0; 
		temp->next() != NULL && i < index;
		temp = temp->next(), i++);
	return temp->Element();
}

template <class e>
e SingleLink<e>::operator [] (int index)
{
	return At(index);
}

template <class e>
int SingleLink<e>::GetSize ()
{
	int i;
	for (temp = head, i = 0; 
		temp != NULL;
		temp = temp->next(), i++);
	return i;
}

#endif